package com.txm.dao;

import com.github.pagehelper.Page;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate5.HibernateCallback;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by Administrator on 2016/5/8.
 */
@Repository
public class BaseDao<T> {
    /*    @Autowired*/
    private HibernateTemplate hibernateTemplate;

    /**
     * 保存或者更新对象
     *
     * @param t
     * @return
     */
    public T merge(T t) {
        t = hibernateTemplate.merge(t);
        return t;
    }

    /**
     * 删除对象
     *
     * @param t
     */
    public void delete(T t) {
        hibernateTemplate.delete(t);
    }

    /**
     * 批量删除
     *
     * @param t
     */
    public void delete(Collection t) {
        hibernateTemplate.deleteAll(t);
    }

    /**
     * 查询对象列表
     *
     * @return
     */
    public List list(String Hql, Map parameters) {
        String[] keys = (String[]) parameters.keySet().toArray(new String[parameters.keySet().size()]);
        Object[] values = parameters.values().toArray();
        List list = hibernateTemplate.findByNamedParam(Hql, keys, values);
        return list;
    }

    public List list(String Hql) {
        List list = hibernateTemplate.find(Hql);
        return list;
    }

    public List list(String Hql, Object... value) {
        List list = hibernateTemplate.find(Hql, value);
        return list;
    }

    /**
     * 查询count
     *
     * @param Hql
     * @return
     */
    public Long count(final String Hql) {
        Long count = hibernateTemplate.execute(new HibernateCallback<Long>() {
            public Long doInHibernate(Session session) throws HibernateException {
                Query query = session.getSessionFactory().getCurrentSession().createQuery(Hql);
                Long count = (Long) query.uniqueResult();
                return count;
            }
        });
        return count;
    }

    /**
     * 查询count
     *
     * @param Hql
     * @param value
     * @return
     */
    public Long count(final String Hql, final Object... value) {
        Long count = hibernateTemplate.execute(new HibernateCallback<Long>() {
            public Long doInHibernate(Session session) throws HibernateException {
                Query query = session.getSessionFactory().getCurrentSession().createQuery(Hql);
                for (int i = 0; i < value.length; i++) {
                    query.setParameter(i, value[i]);
                }
                Long count = (Long) query.uniqueResult();
                return count;
            }
        });
        return count;
    }

    /**
     * 查询count
     *
     * @param Hql
     * @param parameters
     * @return
     */
    public Long count(final String Hql, final Map parameters) {
        return hibernateTemplate.execute(new HibernateCallback<Long>() {
            public Long doInHibernate(Session session) throws HibernateException {
                Query query = session.getSessionFactory().getCurrentSession().createQuery(Hql);
                Set set = parameters.entrySet();
                for (Object s : set) {
                    Map.Entry e = (Map.Entry) s;
                    query.setParameter((String) e.getKey(), e.getValue());
                }
                Long count = (Long) query.uniqueResult();
                return count;
            }
        });
    }

    /**
     * 查询分页
     *
     * @param Hql
     * @return
     */

    public Page page(final String Hql, final Page page) {
        return hibernateTemplate.execute(new HibernateCallback<Page>() {
            public Page doInHibernate(Session session) throws HibernateException {
                org.hibernate.query.Query query = session.getSessionFactory().getCurrentSession().createQuery(Hql);
                query.setFirstResult((page.getPageNum() - 1) * page.getPageSize());
                query.setMaxResults(page.getPageSize());
                List list = query.list();
                page.setPageSize(list.size());
                return page;
            }
        });

    }

    public Page page(final String Hql, final Page page, final Map parameters) {
        return hibernateTemplate.execute(new HibernateCallback<Page>() {
            public Page doInHibernate(Session session) throws HibernateException {
                Query query = session.getSessionFactory().getCurrentSession().createQuery(Hql);
                Set set = parameters.entrySet();
                for (Object s : set) {
                    Map.Entry e = (Map.Entry) s;
                    query.setParameter((String) e.getKey(), e.getValue());
                }
                query.setFirstResult((page.getPageNum() - 1) * page.getPageSize());
                query.setMaxResults(page.getPageSize());
                List list = query.list();
                page.setPageSize(list.size());
                return page;
            }
        });

    }

}
